<?php
	include("library/vars.php");

	if(isset($_POST['submit']))	{ // If the form is submited
	
		$season_name = $_POST['season_name'];
	
		include("library/config.php");
		include("library/opendb.php");
	
		if ($season_name==$season) { // Check to see if we changed the season number
	
			$query = "UPDATE seasons SET season='$season_name' WHERE season='$season' AND show_ID='$show'";
			mysql_query($query);
	
			$query = "UPDATE episodes SET season_ID='$season_name' WHERE season_ID='$season' AND show_ID='$show'";
			mysql_query($query) or die('Database Error');
	
			include 'library/closedb.php';
			header( 'Location: index.php?s=' . $show . '&t=' . $season_name . '&m=season_updated' );
		}
		else { // If we changed the season number, check to see if there's already a season with that number
	
			$query="SELECT * FROM seasons WHERE season='$season_name' AND show_ID='$show'";
			$result = mysql_query($query);
			$num=mysql_numrows($result);
	
			if ($num==0) { // If not, edit the season
				$query = "UPDATE seasons SET season='$season_name' WHERE season='$season' AND show_ID='$show'";
				mysql_query($query);
	
				$query = "UPDATE episodes SET season_ID='$season_name' WHERE season_ID='$season' AND show_ID='$show'";
				mysql_query($query) or die('Database Error');
	
				include 'library/closedb.php';
				header( 'Location: index.php?s=' . $show . '&t=' . $season_name . '&m=season_updated' );
			} 
			else {
				include 'library/closedb.php';
				header( 'Location: edit_season.php?s=' . $show . '&t=' . $season . '&m=season_already_exists2' );
			}
		}
	}
	else { // Show the form, filled with the original season data
		
		include("library/config.php");
		include("library/opendb.php");
		
		$query="SELECT season FROM seasons WHERE season='$season' AND show_ID='$show'";
		$result = mysql_query($query);
		$i=0;
		$season_name=mysql_result($result,$i,"season");
		
		include("library/closedb.php");
		include("library/header.php");
?>
	
<div id="main">
	<div id="seasons">
		<h2><?php echo _("Edit Season"); ?></h2>
	</div>
	
	<div id="messages">
		<?php include ("library/messages.php");	?>
	</div>
	
	<form method="post" id="edit" autocomplete="off" action="">
		<p><label for="season_name"><?php echo _("Season Number"); ?>:</label> <input name="season_name" type="text" id="season_name" style="width:25px" class="{required:true,digits:true}" value="<?=$season_name;?>" /></p>
	 	
		<p class="submit"><input type="submit" name="submit" class="submit" id="submit" value="<?php echo _("Edit Season"); ?>" /> <?php echo _("or"); ?> <a href="index.php?s=<?=$show?>&amp;t=<?=$season?>"><?php echo _("Cancel"); ?></a></p>
	</form>
	
	<div class="push"></div>

</div><!-- /#main -->
<?php include("library/footer.php");?>
</body>
</html>
<?php } ?>